package com.tz.crm.dao.impl;

import com.tz.crm.dao.AnnouncementDao;
import com.tz.crm.entity.Announcement;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 使用 @Repository 将 DAO 类声明为 Bean
 */
@Repository
@Transactional//申明需要进行事务管理,每一个业务方法开始时都会打开一个事务。
@SuppressWarnings("unchecked")
public class AnnouncementDaoImpl extends BaseDaoImpl<Announcement, String>
        implements AnnouncementDao {

    /**
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Announcement> selectAll() {
        String hql = "from Announcement order by createDate desc";
        List<Announcement> list = getSession().createQuery(hql).setFirstResult(0).setMaxResults(6).list();
        return list;
    }

    @Override
    public Announcement getByTitle(String title) {
        String hql = "from Announcement a where a.title=:title";
        return (Announcement) getSession().createQuery(hql).setParameter("title", title).uniqueResult();
    }

    @Override
    public List<Announcement> getAll() {
        String hql = "from Announcement ac order by ac.modifyDate desc";
        List<Announcement> list = getSession().createQuery(hql).list();
        return list;
    }

}
